package com.example.medical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.medical.entity.dto.*;
import com.example.medical.entity.pojo.Leadership;
import com.example.medical.entity.pojo.Storeroom;
import com.example.medical.entity.pojo.StoreroomType;
import com.example.medical.mapper.LeadershipMapper;
import com.example.medical.mapper.StoreroomTypeMapper;
import com.example.medical.service.StoreroomService;
import com.example.medical.mapper.StoreroomMapper;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.PageResponseResult;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
* @author 24354
* @description 针对表【storeroom】的数据库操作Service实现
* @createDate 2024-03-31 22:42:28
*/
@Service
public class StoreroomServiceImpl extends ServiceImpl<StoreroomMapper, Storeroom>
    implements StoreroomService{

    @Autowired
    private StoreroomMapper storeroomMapper;
    @Autowired
    private LeadershipMapper leadershipMapper;
    @Autowired
    private StoreroomTypeMapper storeroomTypeMapper;

    @Override
    public Result UserGetStoreroomList(UserGetStoreroomListDto dto) {
        dto.checkParam();
        IPage<Storeroom> page=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Storeroom> wrapper=new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            wrapper.like(Storeroom::getStoreroomName,dto.getName());
        }
        if (dto.getStoreroomTypeId()!=null) {
            wrapper.like(Storeroom::getStoreroomTypeId,dto.getStoreroomTypeId());
        }
        page=page(page,wrapper);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public Result LeadershipSaveStoreroom(LeadershipSaveStoreroomDto dto) {
        Long leadershipId = BaseContext.getCurrentId();
        Leadership checkLeadership = leadershipMapper.selectById(leadershipId);
        if (leadershipId==null||checkLeadership==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        StoreroomType checkStoreroomType = storeroomTypeMapper.selectById(dto.getTypeId());
        if (checkStoreroomType==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        Storeroom storeroom=new Storeroom();
        storeroom= storeroom.builder()
                .storeroomNumber(dto.getNumber())
                .storeroomTypeName(checkStoreroomType.getStoreroomTypeName())
                .storeroomName(dto.getName())
                .storeroomAddress(dto.getAddress())
                .storeroomMessage(dto.getMessage())
                .storeroomTypeId(dto.getTypeId())
                .saveTime(String.valueOf(LocalDateTime.now()))
                .build();
        boolean checkSave = save(storeroom);
        if (!checkSave) {
            return Result.errorResult(Code.SERVER_ERROR);
        }

        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result LeadershipUpdateStoreroom(LeadershipUpdateStoreroomDto dto) {
        Long leadershipId = BaseContext.getCurrentId();
        Leadership checkLeadership = leadershipMapper.selectById(leadershipId);
        if (leadershipId==null||checkLeadership==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        StoreroomType checkStoreroomType = storeroomTypeMapper.selectById(dto.getTypeId());
        if (checkStoreroomType==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        Storeroom checkStoreroom = storeroomMapper.selectById(dto.getId());
        Storeroom storeroom=new Storeroom();
        BeanUtils.copyProperties(checkStoreroom,storeroom);
        if (StringUtils.isNotBlank(dto.getNumber())) {
            storeroom.setStoreroomNumber(dto.getNumber());
        }
        if (StringUtils.isNotBlank(dto.getName())) {
            storeroom.setStoreroomName(dto.getName());
        }
        if (dto.getTypeId()!=null) {
            storeroom.setStoreroomTypeId(dto.getTypeId());
            storeroom.setStoreroomTypeName(checkStoreroomType.getStoreroomTypeName());
        }
        if (StringUtils.isNotBlank(dto.getAddress())) {
            storeroom.setStoreroomAddress(dto.getAddress());
        }
        if (StringUtils.isNotBlank(dto.getMessage())) {
            storeroom.setStoreroomMessage(dto.getMessage());
        }
        boolean checkUpdate = updateById(storeroom);
        if (!checkUpdate) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result LeadershipRemoveStoreroomArr(String[] ids) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        for (String id:ids) {
            Storeroom checkStoreroom = storeroomMapper.selectById(id);
            if (checkStoreroom==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            boolean checkRemove = removeById(id);
            if (!checkRemove) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result AdminUpdateStoreroom(AdminUpdateStoreroomDto dto) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Storeroom checkStoreroom = storeroomMapper.selectById(dto.getId());
        if (checkStoreroom==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }

        if (StringUtils.isNotBlank(dto.getNumber())) {
            checkStoreroom.setStoreroomNumber(dto.getNumber());
        }
        if (StringUtils.isNotBlank(dto.getName())) {
            checkStoreroom.setStoreroomName(dto.getName());
        }
        if (dto.getTypeId()!=null) {
            StoreroomType checkStoreroomType = storeroomTypeMapper.selectById(dto.getTypeId());
            if (checkStoreroomType==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            checkStoreroom.setStoreroomTypeId(checkStoreroomType.getStoreroomTypeId());
            checkStoreroom.setStoreroomTypeName(checkStoreroomType.getStoreroomTypeName());
        }
        if (StringUtils.isNotBlank(dto.getAddress())) {
            checkStoreroom.setStoreroomAddress(dto.getAddress());
        }
        if (StringUtils.isNotBlank(dto.getMessage())) {
            checkStoreroom.setStoreroomMessage(dto.getMessage());
        }
        boolean checkUpdate = updateById(checkStoreroom);
        if (!checkUpdate) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result AdminSaveStoreroom(AdminSaveStoreroomDto dto) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        if (dto==null) {
            return Result.errorResult(Code.PARAM_INVALID);
        }
        StoreroomType checkStoreroomType = storeroomTypeMapper.selectById(dto.getTypeId());
        if (checkStoreroomType==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        Storeroom storeroom=new Storeroom().builder()
                .storeroomNumber(dto.getNumber())
                .storeroomName(dto.getName())
                .storeroomTypeId(checkStoreroomType.getStoreroomTypeId())
                .storeroomTypeName(checkStoreroomType.getStoreroomTypeName())
                .storeroomAddress(dto.getAddress())
                .storeroomMessage(dto.getMessage()).build();
        boolean checkSave = save(storeroom);
        if (!checkSave) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result AdminRemoveStoreroomArr(String[] ids) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        for (String id : ids) {
            Storeroom checkStoreroom = storeroomMapper.selectById(id);
            if (checkStoreroom==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            boolean checkRemove = removeById(id);
            if (!checkRemove) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
        }
        return Result.okResult(Code.SUCCESS);
    }
}




